﻿using HalconDotNet;
using NeuroCheck.PlugInLib.ImageProcessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CLVisionHalconNC620.Transform
{
    class MeasurementTransform
    {
        ////将NcPIMeasurementCollection对象赋值给NcPIRuntimeData对象
        //OutDataMea.Item = OutMeasure;
        //    m_ExecutionArgs.RuntimeOutputData.Add(OutDataMea);
        //    NcPIMeasurement ncPIMeasurement = new NcPIMeasurement(0, -1, "err");
        //OutMeasure.Add(ncPIMeasurement);

        /// <summary>
        /// 将Halcon数据转换成NcPIMeasurementCollection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="startId"></param>
        /// <param name="value">数据类型：double、Htuple</param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static NcPIMeasurementCollection HcMeasurement2NcPIMeasurement<T>(int startId, T value, string name)
        {
            NcPIMeasurementCollection tempMeasurementCollection = new NcPIMeasurementCollection();
            if (typeof(T) == typeof(double))
            {
                double mValue = Convert.ToDouble(value);
                NcPIMeasurement ncPIMeasurement = new NcPIMeasurement(startId, mValue, name);
                tempMeasurementCollection.Add(ncPIMeasurement);
                return tempMeasurementCollection;
            }
            else if (typeof(T) == typeof(HTuple))
            {
                //int、float 放在测量列表的value，string放在测量列表的description
                HTuple mValue = value as HTuple;
                for (int i = 0; i < mValue.Length; i++)
                {
                    //获取Htuple中存放的数据类型
                    HTupleType valueType = mValue[i].Type;
                    NcPIMeasurement ncPIMeasurement;
                    switch (valueType.ToString().ToLower())
                    {
                        case "integer":
                            ncPIMeasurement = new NcPIMeasurement(startId + i, mValue[i], name);
                            tempMeasurementCollection.Add(ncPIMeasurement);
                            break;
                        case "string":
                            ncPIMeasurement = new NcPIMeasurement(startId + i, -1, mValue[i].ToString());
                            tempMeasurementCollection.Add(ncPIMeasurement);
                            break;
                        case "double":
                            ncPIMeasurement = new NcPIMeasurement(startId + i, mValue[i], name);
                            tempMeasurementCollection.Add(ncPIMeasurement);
                            break;
                        default:
                            break;
                    }
                }
                return tempMeasurementCollection;
            }
            return null;
        }

    }
}
